﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AndroidBridge : MonoBehaviour
{
    private static AndroidBridge instance;
    public static AndroidBridge GetInstance() { return instance; }

    public static string packageName = "com.Ying.ECG";
    private AndroidJavaObject javaObject;

    /// <summary>
    /// 体温回调
    /// </summary>
    private Action<float> temperatureCallback;

    /// <summary>
    /// 心电回调
    /// </summary>
    private Action<int, int, float> heartCallback;

    private void Awake()
    {
        AndroidBridge[] targets = FindObjectsOfType<AndroidBridge>();
        for (int i = 0; i < targets.Length; i++)
        {
            if (targets[i] != this)
            {
                Destroy(gameObject);
                return;
            }
        }
        if (instance == null) { instance = this; }

        javaObject = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
    }

    /// <summary>
    /// 连接时报错回调
    /// </summary>
    /// <param name="callback"></param>
    public void SetErrorCallback(Action<ErrorType, string> callback)
    {
        javaObject.Call("SetGetErrorListener", new GetErrorListener(callback));
    }

    /// <summary>
    /// 选择蓝牙回调
    /// </summary>
    /// <param name="callback"></param>
    public void CheckBluetooth(Action<bool> callback)
    {
        javaObject.Call("CheckBluetooth", new CheckBluetoothListener(callback));
    }

    /// <summary>
    /// 开始扫描
    /// </summary>
    /// <param name="callback"></param>
    public void StartScan(Action<string> callback)
    {
        javaObject.Call("StartScan", new BluetoothScanListener(callback));
    }

    /// <summary>
    /// 停止扫描
    /// </summary>
    public void StopScan()
    {
        javaObject.Call("StopScan");
    }

    /// <summary>
    /// 连接
    /// </summary>
    /// <param name="deviceName"></param>
    public void Connect(string deviceName)
    {
        javaObject.Call("Connect", deviceName, new GetDataListener(GetData));
    }

    public void SetCallback(Action<float> temperature, Action<int, int, float> heart)
    {
        temperatureCallback = temperature;
        heartCallback = heart;
    }

    /// <summary>
    /// 断开连接
    /// </summary>
    /// <param name="name"></param>
    public void DisconnectDevice(string name)
    {
        javaObject.Call("DisconnectDevice", name);
    }

    /// <summary>
    /// 获取数据
    /// </summary>
    /// <param name="type"></param>
    /// <param name="data"></param>
    private void GetData(int type, byte[] data)
    {
        Debug.Log("type =>" + type);
        if (type == 0)//体温
        {
            string temp = "";
            for (int i = 0; i < data.Length; i++) { temp += data[i] + "-"; }
            Debug.Log("temp =>" + temp);
            string flag = Convert.ToString(data[0], 2);//转2进制
            flag = flag.Insert(0, new string('0', 8 - flag.Length));
            Debug.Log("flag =>" + flag);
            string unit = flag[0] == '0' ? "℃" : "K";
            byte[] tData = new byte[4];
            if (BitConverter.IsLittleEndian)
            {
                tData[0] = data[1];
                tData[1] = data[2];
                tData[2] = data[3];
                tData[3] = data[4];
            }
            else
            {
                tData[0] = data[4];
                tData[1] = data[3];
                tData[2] = data[2];
                tData[3] = data[1];
            }
            Debug.Log(BitConverter.IsLittleEndian);
            Debug.Log(tData[0] + " " + tData[1] + " " + tData[2] + " " + tData[3]);
            int t = BitConverter.ToInt32(tData, 0);
            float t2 = (float)t / 100f;
            Debug.Log("温度:" + t2 + unit);//204 13 0 0
            temperatureCallback?.Invoke(t2);
        }
        else
        {
            string temp = "";
            for (int i = 0; i < data.Length; i++)
            {
                temp += data[i] + "-";
                string flag = Convert.ToString(data[i], 16);//转16进制
                //flag = flag.Insert(0, new string('0', 4 - flag.Length));
                Debug.Log("flag =>" + flag);
            }
            Debug.Log("data.Length =>" + data.Length);
            Debug.Log("temp =>" + temp);
        }
    }
}
